//package com.jiyun.filter;
//
////import com.usian.admin.gateway.utils.AppJwtUtil;
////import io.jsonwebtoken.Claims;
////import org.apache.commons.lang3.StringUtils;
//import com.jiyun.utils.AppJwtUtil;
//import io.jsonwebtoken.Claims;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//
//@Component
//public class AuthorizeFilter implements GlobalFilter, Ordered {
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//        //1、判断是否需要鉴权（判断是否是登录/注册请求）
//        if(request.getURI().getPath().contains("/login/")) return chain.filter(exchange);
//        //2、判断是否有token
//        //2.1、获取token：从header中获取
//        String token = request.getHeaders().getFirst("token");
//        if(StringUtils.isEmpty(token)) {
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        //3、判断token是否有效
//        //3.1、通过jwt工具类进行校验
//        Claims claims = AppJwtUtil.getClaimsBody(token);
//        int verifyToken = AppJwtUtil.verifyToken(claims);
//        if(verifyToken==1 || verifyToken==2) {
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            return response.setComplete();
//        }
//        //4、设置id到header
//        //4.1、获取id
//        Integer id = (Integer)claims.get("id");//userID
//        ServerHttpRequest serverHttpRequest = request.mutate().headers(h -> h.add("id", id.toString())).build();
//        exchange.mutate().request(serverHttpRequest).build();
//
//        return chain.filter(exchange);
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}
